//
//  LBRHTTPModule.m
//  LBReact
//
//  Created by WuShiHai on 19/5/24.
//  Copyright (c) 2019 Lebbay. All rights reserved.
//

#import "LBRHTTPModule.h"

#import <LBFoundation/LBFoundation.h>

#import "LBRConstants.h"
#import "LBReactBridge.h"
#import "LBReactBridge+Native.h"

@implementation WRHTTPModule
@synthesize bridge = _bridge;

RCT_EXPORT_MODULE();

RCT_EXPORT_METHOD(http:(NSDictionary *)paramMap succes:(RCTPromiseResolveBlock)resolve fail:(RCTPromiseRejectBlock)reject){
    if ([[LBReactBridge manager].delegate respondsToSelector:@selector(POST:version:parameters:success:fail:)]) {
        [[LBReactBridge manager].delegate POST:[paramMap objectForKey:@"url"]
                                       version:[paramMap objectForKey:@"bundleVersion"]
                                    parameters:paramMap
                                       success:^(NSDictionary *response) {
                                           LBBlockSafeRun(resolve, response);
                                       } fail:^(NSError *error,NSDictionary *response) {
                                           LBBlockSafeRun(reject, [@(error.code) stringValue],[response yy_modelToJSONString], error);
                                       }];
    }
}

RCT_EXPORT_METHOD(upload:(NSDictionary *)paramMap success:(RCTPromiseResolveBlock)resolve fail:(RCTPromiseRejectBlock)reject){
    if ([[LBReactBridge manager].delegate respondsToSelector:@selector(upload:version:filePath:fileKey:parameters:success:fail:)]) {
        [[LBReactBridge manager].delegate upload:[paramMap objectForKey:@"url"]
                                         version:[paramMap objectForKey:@"bundleVersion"]
                                        filePath:[paramMap objectForKey:@"filePath"]
                                         fileKey:[paramMap objectForKey:@"fileKey"]
                                      parameters:paramMap
                                         success:^(NSDictionary *response) {
                                             LBBlockSafeRun(resolve, response);
                                         }
                                            fail:^(NSError *error, NSDictionary *response) {
                                                LBBlockSafeRun(reject, [@(error.code) stringValue],[response yy_modelToJSONString], error);
                                            }];
    }
}

@end
